<!doctype html>
<html>

	<head>
		<meta charset="utf-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="css/mui.css" rel="stylesheet" />
		<link rel="stylesheet" type="text/css" href="css/header.css"/>
		<link rel="stylesheet" type="text/css" href="css/common.css"/>
	</head>

	<body>
		<header class="mui-bar mui-bar-nav title-bar">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left icon-arrowleft"></a>
			<a id="link_submit_username" class="mui-icon mui-icon-right-nav mui-pull-right icon-complete-btn">搜索</a>
			<h1 class="mui-title title-content">添加好友</h1>
		</header>
		<div class="mui-content">
			<div class="mui-input-row">
				<input id="txt_username" type="text" class="mui-input-speech mui-input-clear" placeholder="搜索用户">
			</div>
		</div>
		
		<script src="js/mui.js"></script>
		<script src="js/app.js" type="text/javascript" charset="utf-8"></script>
		<script type="text/javascript">
			mui.init();
			
			mui.plusReady(function() {
				var userinfo = app.getGlobalUserInfo();
				var txt_username = document.getElementById("txt_username");
				
				// 提交事件，搜索朋友
				document.getElementById("link_submit_username").addEventListener("tap", function() {
					var friendUsername = txt_username.value;
					if(!app.isNotNull(friendUsername)) {
						app.showToast("昵称不能为空！", "error");
						return;
					}
					if(friendUsername.length > 8) {
						app.showToast("昵称长度不能超过8位！", "error");
						return;
					}
					
					plus.nativeUI.showWaiting("正在搜索...");
					mui.ajax(app.serverUrl + "/user/searchUserinfo",{
						data:{
							myUserId:userinfo.id,
							friendUsername:friendUsername
						},
						dataType:'json',//服务器返回json格式数据
						type:'post',//HTTP请求类型
						timeout:10000,//超时时间设置为10秒；
						headers:{'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'},	              
						success:function(data){
							plus.nativeUI.closeWaiting();
							// 服务器返回响应，根据响应结果，分析是否登录成功；
							// 控制台中输入日志
							if(data.status == 200) {
								//console.log(JSON.stringify(data.data));
								mui.openWindow({
									url: "searchUserForFriendDetail.html",
									id: "searchUserForFriendDetail.html",
									extras: {
										willBeFriend: data.data
									}
								});
							} else {
								app.showToast(data.msg, "error");
							}
						},
						error:function(xhr,type,errorThrown){
							//异常处理；
							console.log(type);
						}
					});
				});
			});
		</script>
	</body>

</html>
